package uml.behavioral.responsibility_chain;

/**
 * 职责链--抽象处理者<br>
 * on 2023/1/1 1:27
 */
public abstract class Approver {
    protected Approver nextApprover;
    protected String name;
    protected String job;

    public Approver(String name) {
        this.name = name;
    }

    public void setNextApprover(Approver nextApprover) {
        this.nextApprover = nextApprover;
    }

    /**
     * 对请求进行处理，不能处理则应把请求交由下家处理
     *
     * @param request 沿职责链传递的请求
     */
    public abstract void dealWith(PurchaseRequest request);

    /**
     * 同意请求
     *
     * @param request
     */
    protected void agree(PurchaseRequest request) {
        StringBuilder sb = new StringBuilder();
        sb.append("由").append(this.job).append('[').append(this.name).append(']').append("审批\n订单[")
                .append("金额：").append(request.getAmount()).append("万元").append(',')
                .append("编号：").append(request.getId()).append(',')
                .append("用途：").append(request.getPurpose()).append("]")
                .append("\n意见：审批通过\n");
        System.out.println(sb.toString());
    }

    /**
     * 拒绝请求
     *
     * @param request
     */
    protected void refuse(PurchaseRequest request) {
        StringBuilder sb = new StringBuilder();
        sb.append("即使由").append(this.job).append('[').append(this.name).append(']').append("审批\n订单[")
                .append("金额：").append(request.getAmount()).append("万元").append(',')
                .append("编号：").append(request.getId()).append(',')
                .append("用途：").append(request.getPurpose()).append("]")
                .append("\n结果：审批也未通过,理由是订单金额超过公司最大预算100万\n");
        System.out.println(sb.toString());
    }
}
